﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.Enums;
//using AutoMapper;

namespace Zocono.WCS.Domain.RouteDomain
{
    public class RouteDetailInfoService : IRouteDetailInfoService
    {
        private readonly ILogUtil _LogUtil;
        private readonly IRedisHelper _IRedisHelper;
        private readonly ISnowflake _Snowflake;
        private readonly IRouteDetailInfoRepository _RouteDetailInfoRepository;
        //private readonly Mapper _Mapper;
        public RouteDetailInfoService(ILogUtil logUtil
            , IRedisHelper iRedisHelper
            , ISnowflake snowflake
            , IRouteDetailInfoRepository routeDetailInfoRepository
            /*,Mapper mapper*/)
        {
            _LogUtil = logUtil;
            _IRedisHelper = iRedisHelper;
            _Snowflake = snowflake;
            _RouteDetailInfoRepository = routeDetailInfoRepository;
            //_Mapper = mapper;
        }

        public async Task<bool> CreateRouteDetailInfoAsync(RouteDetailInfo routeDetailInfo)
        {
            routeDetailInfo.ID = _Snowflake.GetId();
            var isOk = await _RouteDetailInfoRepository.InsertAsync(routeDetailInfo);

            return isOk;
        }

        public async Task<bool> DeleteRouteDetailInfoAsync(List<RouteDetailInfo> routeDetailInfoList)
        {
            try
            {
                _RouteDetailInfoRepository.AsTenant().BeginTran();

                foreach (var item in routeDetailInfoList)
                {
                    await _RouteDetailInfoRepository.DeleteByIdAsync(item.ID);
                }

                _RouteDetailInfoRepository.AsTenant().CommitTran();

                return true;
            }
            catch (Exception ex)
            {
                _RouteDetailInfoRepository.AsTenant().RollbackTran();
                throw new Exception(ex.Message);
            }
        }

        public async Task<bool> DeleteRouteDetailInfoByIdAsync(long id)
        {
            return await _RouteDetailInfoRepository.DeleteByIdAsync(id);
        }

        public async Task<PageDto<RouteDetailInfoDto>> GetPagedRouteDetailInfoAsync(QueryRouteDetailInfoDto queryRouteDetailInfo)
        {
            var exp = Expressionable.Create<RouteDetailInfo, RouteInfo>();
            exp.AndIF(queryRouteDetailInfo.RouteID.HasValue, (rdi, ri) => rdi.RouteID == queryRouteDetailInfo.RouteID);
            exp.AndIF(!string.IsNullOrEmpty(queryRouteDetailInfo.FromStation), (rdi, ri) => ri.FromStation.Contains(queryRouteDetailInfo.FromStation));
            exp.AndIF(!string.IsNullOrEmpty(queryRouteDetailInfo.ToStation), (rdi, ri) => ri.ToStation.Contains(queryRouteDetailInfo.ToStation));
            exp.AndIF(queryRouteDetailInfo.IsStart.HasValue, (rdi, ri) => rdi.IsStart == queryRouteDetailInfo.IsStart);
            exp.AndIF(queryRouteDetailInfo.IsEnd.HasValue, (rdi, ri) => rdi.IsEnd == queryRouteDetailInfo.IsEnd);
            exp.AndIF(queryRouteDetailInfo.DispatchType.HasValue, (rdi, ri) => rdi.DispatchType == Enum.Parse<EnumDispatchType>(queryRouteDetailInfo.DispatchType.ToString()));
            exp.AndIF(!string.IsNullOrEmpty(queryRouteDetailInfo.FromEquipment), (rdi, ri) => rdi.FromEquipment.ToString().Contains(queryRouteDetailInfo.FromEquipment));
            exp.AndIF(!string.IsNullOrEmpty(queryRouteDetailInfo.FromCargo), (rdi, ri) => rdi.FromCargo.Contains(queryRouteDetailInfo.FromCargo));
            exp.AndIF(!string.IsNullOrEmpty(queryRouteDetailInfo.ToEquipment), (rdi, ri) => rdi.ToEquipment.ToString().Contains(queryRouteDetailInfo.ToEquipment));
            exp.AndIF(!string.IsNullOrEmpty(queryRouteDetailInfo.ToCargo), (rdi, ri) => rdi.ToCargo.Contains(queryRouteDetailInfo.ToCargo));

            PageDto<RouteDetailInfoDto> pagedData = new();
            RefAsync<int> totalCount = 0;

            var result = await _RouteDetailInfoRepository.AsSugarClient()
                .Queryable<RouteDetailInfo, RouteInfo>((rdi, ri) => new JoinQueryInfos(JoinType.Inner, rdi.RouteID == ri.ID))
                .Where(exp.ToExpression())
                .Select((rdi, ri) => new RouteDetailInfoDto()
                {
                    ID = rdi.ID,
                    PrevionsID = rdi.PrevionsID,
                    RouteID = ri.ID,
                    FromStation = ri.FromStation,
                    ToStation = ri.ToStation,
                    IsStart = rdi.IsStart,
                    IsEnd = rdi.IsEnd,
                    DispatchType = rdi.DispatchType,
                    FromEquipment = rdi.FromEquipment.ToString(),
                    FromCargo = rdi.FromCargo,
                    FromCargoColumn = rdi.FromCargoColumn,
                    ToEquipment = rdi.ToEquipment.ToString(),
                    ToCargo = rdi.ToCargo
                })
                .ToPageListAsync(queryRouteDetailInfo.PageIndex, queryRouteDetailInfo.PageSize, totalCount);

            pagedData.Result = result;
            pagedData.Total = totalCount;

            return pagedData;
        }

        public async Task<List<RouteDetailInfo>> GetRouteDetailInfoByDetailIDAsync(long routeDetailInfoId)
        {
            var exp = Expressionable.Create<RouteDetailInfo>();
            exp.AndIF(true, it => it.PrevionsID == routeDetailInfoId);

            var list = await _RouteDetailInfoRepository.GetListAsync(exp.ToExpression());
            return list;
        }

        public async Task<RouteDetailInfo> GetRouteDetailInfoByIdAsync(long Id)
        {
            var routeDetailInfo = await _RouteDetailInfoRepository.AsSugarClient()
                 .Queryable<RouteDetailInfo>().FirstAsync(it => it.ID == Id);
            if (routeDetailInfo == null)
            {
                throw new Exception($"路径详细ID: {Id}没有配置数据");
            }
            return routeDetailInfo;
        }

        public async Task<List<RouteDetailInfo>> GetRouteDetailInfoByRouteIDAsync(long routeId)
        {
            var exp = Expressionable.Create<RouteDetailInfo>();
            exp.AndIF(true, it => it.RouteID == routeId);

            var list = await _RouteDetailInfoRepository.GetListAsync(exp.ToExpression());
            return list;
        }

        public async Task<bool> ModifyRouteDetailInfoAsync(RouteDetailInfo routeDetailInfo)
        {
            bool isOk = await _RouteDetailInfoRepository.UpdateAsync(routeDetailInfo);

            return isOk;
        }

        public async Task<List<RouteDetailInfo>> GetRouteDetailInfoListAsync()
        {
            List<RouteDetailInfo> list = new();
            list = _IRedisHelper.GetStringKey<List<RouteDetailInfo>>("RouteDetailInfo");
            if (list == null || list.Count == 0)
            {
                list = await _RouteDetailInfoRepository.GetListAsync();
                _IRedisHelper.SetStringKey<List<RouteDetailInfo>>("RouteDetailInfo", list);
            }
            return list;
        }

        public async Task<long> CreateRouteDetailInfoOutIdAsync(RouteDetailInfo routeDetailInfo)
        {
            routeDetailInfo.ID = _Snowflake.GetId();
            var isOk = await _RouteDetailInfoRepository.InsertAsync(routeDetailInfo);
            if (isOk)
            {
                return routeDetailInfo.ID;
            }
            return 0;
        }
    }
}
